package dao

import (
	"context"
	"go.uber.org/zap"
	"mall-portalv2/global"
	"mall-portalv2/internal/model"
)

func (d *Dao) UpdateOmsOrderReturnApply(ctx context.Context, id int64, updates map[string]interface{}) error {
	tb := model.OmsOrderReturnApply{}
	if err := d.db.WithContext(ctx).Table(tb.TableName()).Where("id = ?", id).Updates(updates).Error; err != nil {
		return err
	}
	return nil
}

func (d *Dao) UpdateOmsOrderReturnApplyWhereOrderId(ctx context.Context, Order int64, updates map[string]interface{}) error {
	tb := model.OmsOrderReturnApply{}
	if err := d.db.WithContext(ctx).Table(tb.TableName()).Where("order_id = ?", Order).Updates(updates).Error; err != nil {
		return err
	}
	return nil
}

func (d *Dao) QueryOmsOrderReturnApplyCount(ctx context.Context, Uid int64) (int64, error) {
	tb := model.OmsOrderReturnApply{}
	var result int64
	if err := d.db.WithContext(ctx).Table(tb.TableName()).Where("member_id = ? AND status not in (2,3,4,6,8)", Uid).Find(&tb).Count(&result).Error; err != nil {
		return result, err
	}
	return result, nil
}

func (d *Dao) QueryOmsOrderReturnApplyWhereID(ctx context.Context, id int64) (model.OmsOrderReturnApply, error) {
	tb := model.OmsOrderReturnApply{}
	var result model.OmsOrderReturnApply
	if err := d.db.WithContext(ctx).Table(tb.TableName()).Where("id = ?", id).First(&result).Error; err != nil {
		return result, err
	}
	global.Log.Info("打印一下查询的结果", zap.Any("data", result))
	return result, nil
}

func (d *Dao) QueryViewOmsOrderReturnApplyList(ctx context.Context, memberId int64, limit, offset int) ([]model.ViewOmsOrderReturnApply, error) {
	tb := model.ViewOmsOrderReturnApply{}
	result := []model.ViewOmsOrderReturnApply{}
	// 订单状态：0->待付款；1->待发货；2->已发货；3->已完成；4->已关闭；5->无效订单
	if err := d.db.WithContext(ctx).Table(tb.TableName()).Where("member_id = ?", memberId).Limit(limit).Offset(offset).Order("id DESC").Find(&result).Error; err != nil {
		return result, err
	}
	return result, nil
}

func (d *Dao) QueryViewOmsOrderReturnApplyListCount(ctx context.Context, memberId int64) (int64, error) {
	tb := model.ViewOmsOrderReturnApply{}
	var (
		result int64
	)
	// 订单状态：0->待付款；1->待发货；2->已发货；3->已完成；4->已关闭；5->无效订单
	if err := d.db.WithContext(ctx).Table(tb.TableName()).Where("member_id = ?", memberId).Find(&tb).Count(&result).Error; err != nil {
		return result, err
	}
	return result, nil
}

func (d *Dao) QueryViewOmsOrderReturnApplyWhereOrderSn(ctx context.Context, Uid int64) (model.OmsOrderReturnApply, error) {
	tb := model.OmsOrderReturnApply{}
	result := model.OmsOrderReturnApply{}
	if err := d.db.WithContext(ctx).Table(tb.TableName()).Where("member_id = ? AND status not in (2,3,4,6)", Uid).Find(&result).Error; err != nil {
		return result, err
	}
	return result, nil
}
